<template>
  <div class="register-container">
    <div class="register-bg">
      <!-- LOGO -->
      <div class="logo-container">
        <img src="@/assets/images/logo.jpg" alt="全都有超市" class="logo-image">
        <h1 class="logo-text">全都有超市</h1>
      </div>

      <h2 class="register-title">注册新账号</h2>
      <p class="register-desc">填写以下信息开始您的购物之旅</p>

      <!-- 表单 -->
      <el-form ref="form" :model="registerForm" :rules="rules" label-width="90px" class="register-form">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="registerForm.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="registerForm.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="registerForm.password" show-password placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="registerForm.confirmPassword" show-password placeholder="请再次输入密码" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="registerForm.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="registerForm.email" placeholder="请输入邮箱地址" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" class="register-btn" :loading="loading">
            <i class="el-icon-user-plus"></i>
            立即注册
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="login-extra">
        <span>已有账号？<a @click="goToLogin">立即登录</a></span>
      </div>
    </div>
  </div>
</template>

<script>
import api from '@/api/login'
import { ElMessage } from 'element-plus'

export default {
  data() {
    return {
      registerForm: {
        name:'',
        username: '',
        password: '',
        confirmPassword: '',
        phone: '',
        email: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在1到15个字符之间', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 2, max: 20, message: '密码长度在2到20个字符之间', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { 
            pattern: /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/, 
            message: '邮箱格式不正确',
            trigger: 'blur' 
          }
        ]
      },
      loading: false
    }
  },
  methods: {
    async onSubmit() {
  try {
    const {
      name,
      username,
      password,
      confirmPassword,
      phone,
      email
    } = this.registerForm;

    if (password !== confirmPassword) {
      ElMessage.error('两次输入的密码不一致');
      return;
    }
    console.log('注册信息:', { name, username, phone,email })
    // 设置当前时间
    const now = new Date().toISOString();  
    this.registerForm.uCreateTime = now;
    this.registerForm.uUpdateTime = now;

    this.loading = true;

    const response = await api.register(this.registerForm);

    if (response.data.code === 200) {
      ElMessage.success('注册成功！正在跳转登录页...');
      setTimeout(() => {
        this.$router.push('/login');
      }, 1500);
    } else {
      ElMessage.error(response.data.message || '注册失败');
    }
  } catch (error) {
    console.error('注册失败:', error);
    ElMessage.error('网络异常，请稍后再试');
  } finally {
    this.loading = false;
  }
},
    goToLogin() {
      this.$router.push('/login')
    }
  }
}
</script>

<style lang="less" scoped>
@primary-color: #ff4d4f;
@bg-white-transparent: rgba(255, 255, 255, 0.9);
@border-radius: 8px;

.register-container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat center center fixed;
  background-size: cover;
  display: flex;
  justify-content: center;
  align-items: center;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

.register-bg {
  width: 400px;
  padding: 40px 30px;
  border-radius: @border-radius;
  background-color: @bg-white-transparent;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.1);
  text-align: center;
  backdrop-filter: blur(10px);
}

.logo-container {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 30px;

  .logo-image {
    width: 40px;
    height: 40px;
    margin-right: 10px;
    border-radius: 50%;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  }

  .logo-text {
    font-size: 20px;
    color: @primary-color;
    font-weight: bold;
    letter-spacing: 1px;
    margin: 0;
  }
}

.register-title {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin-bottom: 8px;
}

.register-desc {
  font-size: 14px;
  color: #666;
  margin-bottom: 30px;
}

.register-form {
  width: 100%;

  .register-btn {
    width: 100%;
    background-color: @primary-color;
    border-color: @primary-color;
    font-size: 16px;
    font-weight: 500;
    transition: all 0.3s ease;
    border-radius: @border-radius;

    &:hover {
      background-color: darken(@primary-color, 10%);
      border-color: darken(@primary-color, 10%);
    }

    i {
      margin-right: 8px;
    }
  }
}

.login-extra {
  display: flex;
  justify-content: flex-end;
  width: 100%;
  font-size: 13px;
  color: #999;

  a {
    color: @primary-color;
    text-decoration: none;
    margin: 0 5px;

    &:hover {
      text-decoration: underline;
    }
  }
}
</style>